package com.agricultural.machine.factory;

import com.agricultural.machine.common.AbstractMachine;
import com.agricultural.machine.environment.EnvironmentData;
import com.agricultural.machine.messaging.Message;

/**
 * 拖拉机类
 */
public class TractorMachine extends AbstractMachine {
    private double speed; // 速度 (km/h)
    private String attachedImplement; // 连接的农具
    private boolean stopped; // 是否停止前进
    
    public TractorMachine() {
        super("tractor");
        this.speed = 10.0; // 默认速度10km/h
        this.attachedImplement = "无";
        this.stopped = false;
    }
    
    @Override
    public void work() {
        if (isRunning()) {
            if (stopped) {
                System.out.println("拖拉机 " + getMachineId() + " 已暂时停止前进");
            } else {
                System.out.println("拖拉机 " + getMachineId() + " 正在行驶，速度: " + speed + " km/h, 连接农具: " + attachedImplement);
            }
        } else {
            System.out.println("拖拉机 " + getMachineId() + " 未启动，无法工作");
        }
    }
    
    /**
     * 设置速度
     */
    public void setSpeed(double speed) {
        if (speed < 0.1) {
            speed = 0.1;
        } else if (speed > 30.0) {
            speed = 30.0;
        }
        
        this.speed = speed;
        System.out.println("拖拉机 " + getMachineId() + " 已调整速度为 " + speed + " km/h");
    }
    
    /**
     * 连接农具
     */
    public void attachImplement(String implement) {
        this.attachedImplement = implement;
        System.out.println("拖拉机 " + getMachineId() + " 已连接农具: " + implement);
    }
    
    /**
     * 停止前进
     */
    public void stopMoving() {
        this.stopped = true;
        System.out.println("拖拉机 " + getMachineId() + " 已停止前进");
    }
    
    /**
     * 继续前进
     */
    public void resumeMoving() {
        this.stopped = false;
        System.out.println("拖拉机 " + getMachineId() + " 继续前进");
    }
    
    /**
     * 根据环境数据自动调整操作
     */
    @Override
    protected void automaticAdjustOperation(EnvironmentData environmentData) {
        if (!isRunning()) {
            return;
        }
        
        // 根据环境调整速度
        double soilMoisture = environmentData.getSoilMoisture();
        double windSpeed = environmentData.getWindSpeed();
        
        // 根据土壤湿度调整速度
        double newSpeed = this.speed;
        
        if (soilMoisture > 70) {
            // 土壤过湿，减速
            newSpeed = 5.0;
            System.out.println("拖拉机 " + getMachineId() + " 检测到土壤湿度较高 (" + soilMoisture + "%)，自动减速");
        } else if (windSpeed > 8.0) {
            // 风速过大，减速
            newSpeed = 7.0;
            System.out.println("拖拉机 " + getMachineId() + " 检测到风速较大 (" + windSpeed + "m/s)，自动减速");
        } else if (!environmentData.isObstacleDetected() && soilMoisture < 60 && windSpeed < 5.0) {
            // 良好条件，可以正常速度
            newSpeed = 10.0;
        }
        
        // 只有当需要改变速度时才设置
        if (Math.abs(newSpeed - speed) > 0.1) {
            setSpeed(newSpeed);
        }
    }
    
    /**
     * 接收消息
     */
    @Override
    public void receiveMessage(Message message) {
        super.receiveMessage(message);
        
        // 处理特定的消息类型
        if ("tractor_stop_request".equals(message.getType()) && isRunning()) {
            stopMoving();
            System.out.println("拖拉机 " + getMachineId() + " 收到停止请求: " + message.getContent());
            
            // 三秒后自动尝试继续前进
            new Thread(() -> {
                try {
                    Thread.sleep(3000);
                    if (isAutoOperationEnabled() && isRunning() && stopped) {
                        resumeMoving();
                        System.out.println("拖拉机 " + getMachineId() + " 环境已改善，继续前进");
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }).start();
        }
    }
}








































